package com.gxuwz.cyhschool;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;


public class login extends AppCompatActivity implements View.OnClickListener{

    private EditText userName, password;
    private Button login;

    private String path = "http://172.20.10.2:4523/m1/2815448-0-default/login";

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        getSupportActionBar().hide();
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        userName = findViewById(R.id.userName);
        password = findViewById(R.id.password);
        login = findViewById(R.id.login);
        login.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.login:
                String userName = this.userName.getText().toString();
                String password = this.password.getText().toString();
                if (userName.equals("") || userName == null) {
                    Toast.makeText(login.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                    break;
                } else if (password.equals("") || password == null) {
                    Toast.makeText(login.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                    break;
                }
                try {
                    if (userName.equals("admin") && password.equals("admin")) {
                        intent.setClass(login.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    } else {
                        Toast.makeText(login.this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

}